<?php
namespace app\controller;

use think\Request;
use app\common\UtilService;
use app\model\BbsType;
use app\model\Bbs as BbsModel;
use app\model\User;
use think\facade\Db;

class Bbs extends Base
{
	/**
	 * [detail bbs动态详细]
	 * @since  2022-04-27
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function detail(Request $request)
	{
		$param = UtilService::getMore([
            ['user_id', $request->userId], 
            ['bbs_id', ''],
        ]);

        BbsModel::where('id', $param['bbs_id'])->inc('browse')->update();

        return view('detail', compact('param'));
	}

	/**
	 * [ajaxBbsInfo 异步获取bbs详细，即一条]
	 * @since  2022-04-27
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function ajaxBbsInfo(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['user_id', $request->userId], 
	            ['bbs_id', ''],
	        ]);
	        // dump($param);exit;

			$info = BbsModel::getBbsDetailInfo($param);

			return success($info, '成功');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [index 程序员圈子首页]
	 * @since  2022-04-01
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function index(Request $request)
	{
		$type_list = BbsType::getBbsTypeList();
		
		$userHomeInfo = '';
		
		if($request->userId){
			$userHomeInfo = User::getUserHomeInfo([
				'id' => $request->userId
			]);
		}

		# 活跃用户
		$hy_user = User::whereNull('delete_time')->field('id, nickname, avatar')->order('update_time DESC')->limit(12)->select();
		// dump($userHomeInfo->toArray());
		return view('index', compact('type_list', 'userHomeInfo', 'hy_user'));
	}


	/**
	 * [ajaxBbsTypeList 异步获取话题或者动态分类成功]
	 * @since  2022-04-01
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function ajaxBbsTypeList(Request $request)
	{
		try {

			$list = BbsType::getBbsTypeList();

			return success($list, '获取话题成功');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [ajaxBbsList 异步获取动态列表]
	 * @since  2022-04-06
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function ajaxBbsList(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['user_id', $request->userId], 
	            ['page', 1],
	            ['limit', 10],
	            ['type_id', ''],
	            ['mval', ''],
	        ]);

			$list = BbsModel::getBbsList($param);

			return success($list, '获取动态列表');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}

	/**
	 * [circle_list 全部圈子分类列表]
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function circle_list(Request $request)
	{
		$param = UtilService::getMore([
            ['user_id', $request->userId], 
        ]);

		$list = BbsType::getBbsTypeList();
		// dump($list->toArray());exit;

		return view('circle_list', compact('list', 'param'));
	}

	/**
	 * [circle 圈子动态列表]
	 * @since  2022-04-29
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function circle(Request $request)
	{
		$param = UtilService::getMore([
            ['user_id', $request->userId], 
            ['id', '']
        ]);

        return view('circle', compact('param'));
	}

	/**
	 * [circle 创建圈子]
	 * @since  2022-04-29
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function circle_add(Request $request)
	{
		$param = UtilService::getMore([
            ['user_id', $request->userId], 
            ['id', '']
        ]);

        return view('circle_add', compact('param'));
	}

	/**
	 * [circle_edit 编辑圈子信息]
	 * @since  2022-05-06
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function circle_edit(Request $request)
	{
		$param = UtilService::getMore([
            ['user_id', $request->userId], 
            ['id', ''],
            ['type_ids' , '']
        ]);

        if($request->isAjax()){
        	$map = UtilService::getMore([
	            ['id', ''],
            	['type_ids' , '']
	        ]);

	        $info = BbsType::where($map)->field('id, type_class, type_desc, type_name, type_pic, type_status, type_tags')->find();

	        if($info){
	        	$info = $info->getData();
	        }

	        return success($info, 'ok');
        }

        return view('circle_edit', compact('param'));
	}


	/**
	 * [circleInfo 根据圈子ID查询圈子详细]
	 * @since  2022-05-05
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function circleInfo(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['user_id', $request->userId], 
	            ['id', ''],
	        ]);

	        if($param['id'] == ''){
	        	return error('圈子ID有误');
	        }

			$info = BbsType::getCircleInfo($param);

			$info['login_user_id'] = $request->userId;


			return success($info, '获取圈子详细成功');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}


	/**
	 * [getCircleTotal 根据当前分类的ID查出当前分类下动态的评价总数和点赞总数]
	 * @since  2022-05-05
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function getCircleTotal(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['id', ''],
	        ]);
			
			$sql ="SELECT
					count(DISTINCT b.id) as comment_total,
					count(DISTINCT c.id) as like_total
				FROM
					ea_bbs a 
				LEFT JOIN ea_bbs_comment b ON b.bbs_id = a.id
				LEFT JOIN ea_bbs_like c ON c.bbs_id = a.id
				WHERE
					a.type_id = '".$param['id']."'"
			;

			$info = Db::query($sql);

			return success($info[0], '获取成功');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}
}